#include "dataserver.h"
#include "utils/logger.h"
#include "main.h"
#include <QDir>
#include <QVariant>

DataServer::DataServer(QObject *parent) :
    QObject(parent)
{
    createConnection();
}
bool DataServer::createConnection()
{
    QString dataDir = QDir::homePath();
    QDir dir(dataDir);

    QString dbName = dir.filePath("tl2.sqlite");
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbName);

    if (!db.open()) {
        _log->LogIt(tr("Error al abrir/crear base de datos"),Logg::LERROR);
      return false;
    }

    QSqlQuery query;
    query.exec("CREATE TABLE IF NOT EXIST [account] ([id] integer  PRIMARY KEY AUTOINCREMENT NULL, [username] varchar(50)  UNIQUE NOT NULL,"
    "[password] varchar(64)  NOT NULL,[salt] VARCHAR(53)  NOT NULL)");
    query.exec("CREATE TABLE IF NOT EXIST [friends] ([plid] INTEGER  NOT NULL,[fndid] INTEGER  NOT NULL)");
    query.exec("CREATE TABLE IF NOT EXIST [blocked] ([plid] INTEGER  NOT NULL,[blkid] INTEGER  NOT NULL)");

    return true;
}
quint32 DataServer::GetAccID(QString name)
{
    QSqlQuery query;
    QString sql("select * from account where username == upper('%1');");
    query.prepare(sql.arg(name));
    if(query.exec())
    {
        query.first();
        return (quint32) query.value(0).toUInt();
    }else{
         sql="insert into account values(NULL,upper('%1'), '%2')";
        query.prepare(sql.arg(name).arg("nopass"));
        if(query.exec())
        {
            sql="select * from account where username == upper('%1');";
            query.prepare(sql.arg(name));
            query.exec();
            query.first();
            return (quint32) query.value(0).toUInt();
        }
    }
    return 0;
}
QStringList DataServer::GetFriends(int plid){
    QSqlQuery query;
    QStringList ret;

    QString sql("select username from account where id in(select fndid from friends where plid == %1)");
    query.prepare(sql.arg(plid));

    if(query.exec())
    {
        query.first();
        while(query.next())
            ret.append(query.value(0).toString());
    }
    return ret;
}
QStringList DataServer::GetBlocked(int plid){
    QSqlQuery query;
    QStringList ret;

    QString sql("select username from account where id in(select fndid from blocked where plid == %1)");
    query.prepare(sql.arg(plid));

    if(query.exec())
    {
        query.first();
        while(query.next())
            ret.append(query.value(0).toString());
    }
    return ret;
}
bool DataServer::AddUser(QString username, QString password, QString salt)
{
    QSqlQuery query;
    QString sql("insert into account values(null,upper('%1'), '%2','%3')");
    query.prepare(sql.arg(username).arg(password).arg(salt));

    if(query.exec())
        return true;
    return false;
}
bool DataServer::AddFriend(QString pid, QString fid)
{
    QSqlQuery query;
    QString sql("insert into friends values(%1, %2)");
    query.prepare(sql.arg(pid).arg(fid));

    if(query.exec())
        return true;
    return false;
}
bool DataServer::AddBlock(QString pid, QString bid)
{
    QSqlQuery query;
    QString sql("insert into blocked values(%1, %2)");
    query.prepare(sql.arg(pid).arg(bid));

    if(query.exec())
        return true;
    return false;
}
bool DataServer::RemoveFriend(QString pid, QString fid){
    QSqlQuery query;
    QString sql("delete from friends where fndid == %1 and pid == plid");
    query.prepare(sql.arg(pid).arg(fid));

    if(query.exec())
        return true;
    return false;
}
bool  DataServer::RemoveBlock(QString pid, QString bid){
    QSqlQuery query;
    QString sql("delete from blocked where blkid == %1 and pid == plid");
    query.prepare(sql.arg(pid).arg(bid));

    if(query.exec())
        return true;
    return false;
}
bool DataServer::CheckLogin(quint32 userid, QString password)
{
    QSqlQuery query;
    QString sql("select password from account where id == '%1' AND password == '%2'");
    query.prepare(sql.arg(userid).arg(password));

    if(query.exec() && query.at()>-2)
        return true;
    return false;
}
QString DataServer::GetSalt(QString username)
{
    QSqlQuery query;
    QString sql("select salt from account where username == upper('%1')");
    query.prepare(sql.arg(username));

    if(query.exec() && query.at()>-2)
    {
        query.first();
        QString ret(query.value(0).toString());
        ret.insert(0,(char)0x14); //size of first salt
        return ret;
    }
    return QString::null;

}
